//
//  ZSOAuthController.m
//  微博-0C
//
//  Created by 周李波 on 16/7/21.
//  Copyright © 2016年 q. All rights reserved.
//

#import "ZSOAuthController.h"
#import "ZSBarButtonItem.h"
#import "SVProgressHUD.h"
#import "ZSProgressView.h"

@interface ZSOAuthController ()<UIWebViewDelegate>

@property (nonatomic,strong)UIWebView *webView;

@property (nonatomic,weak)ZSProgressView *progressView;

@end

@implementation ZSOAuthController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupUI];
    [self loadWebView];
    
}
#pragma mark
#pragma mark - 设置UI
- (void)setupUI{
    self.navigationItem.leftBarButtonItem = [ZSBarButtonItem itemWithImageName:nil title:@"取消" target:self action:@selector(cancelClick)];
    self.navigationItem.rightBarButtonItem = [ZSBarButtonItem itemWithImageName:nil title:@"自动填充" target:self action:@selector(fillClick)];
    self.navigationItem.title = @"微博登陆";
    [self.view addSubview:self.webView];
    self.webView.delegate = self;
    ZSProgressView *pro = [[ZSProgressView alloc]initWithFrame:CGRectMake(0, 64, SCREENWIDTH, 2)];
    self.progressView = pro;
    [self.navigationController.view insertSubview:pro belowSubview:self.navigationController.navigationBar];
    
}
#pragma mark
#pragma mark - 加载webview
- (void)loadWebView{
    NSString *urlString = [NSString stringWithFormat:@"https://api.weibo.com/oauth2/authorize?client_id=%@&redirect_uri=%@",APPKEY,APPREDIRECT_URI];
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
   
}
#pragma mark
#pragma mark - 取消按钮点击
- (void)cancelClick{
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
#pragma mark
#pragma mark - 填充按钮点击
- (void)fillClick{
//注入JS
    NSString *jsString = [NSString stringWithFormat:@"document.getElementById('userId').value='%@',document.getElementById('passwd').value='%@'",WEIBONAME,WEIBOPASSWORD];
    [self.webView stringByEvaluatingJavaScriptFromString:jsString];
    
}
#pragma mark
#pragma mark - 代理方法
//开始加载
- (void)webViewDidStartLoad:(UIWebView *)webView{
    [self.progressView startAnimation];
    [SVProgressHUD show];
}
//完成加载
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [SVProgressHUD dismiss];
    [self.progressView endAnimatino];
    [self.progressView removeAnimation];
}
//加载错误
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    [SVProgressHUD showErrorWithStatus:@"错误"];
}
//加载
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    NSString *urlString = request.URL.absoluteString;
    if ([urlString hasPrefix:APPREDIRECT_URI]) {
        NSString *query =  request.URL.query;
        
        NSString *code = [query substringFromIndex:5];
        NSLog(@"%@",code);
    
    }
    return YES;
}

#pragma mark
#pragma mark - 懒加载
- (UIWebView *)webView{
    if (_webView == nil) {
        _webView = [[UIWebView alloc]initWithFrame:self.view.bounds];
        
    }
    return _webView;
}

@end
